﻿using ExampleService.Xxxxx.SDK.Contract;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace ExampleService.Xxxxx.SDK
{
    public static class ExampleServiceSDKExtensions
    {
        /// <summary>
        /// 注册契约接口 
        /// </summary>
        /// <param name="services"></param>
        /// <param name="baseUri"></param>
        public static void AddExampleServiceSDK(this IServiceCollection services, string baseUri = "http://localhost:5208")
        {
            var httpApiTypes = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => type.IsInterface && type.IsPublic && type.IsDefined(typeof(ContractAttribute), false));

            // 注册 获取token 
            services.AddHttpApi<IOAuthContract>(opt =>
            {
                opt.HttpHost = new Uri("http://localhost:5209"); // 设置默认的 HttpHost
            });


            foreach (var httpApiType in httpApiTypes)
            {
                var method = typeof(HttpApiExtensions).GetMethod("AddHttpApi", new[] { typeof(IServiceCollection), typeof(Type), typeof(string) });
                method?.Invoke(null, new object[] { services, httpApiType, baseUri });
            }

            services.AddTokenProvider<IBaseAuthApi, AuthTokenProvider>();

        }

    }
}
